﻿using ComUIControl;
using DYPSensor;
using KWindow.KPageCore;
using LogLib;
using MotionBroker;
using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace IPCPages
{
    public partial class PageTakeAiming : PageControlBase
    {     
        private Aiming _aiming = null;

        private MotionControl _motion = MotionControl.Instance;

        public PageTakeAiming(string lan)
        {
            LanguageCode = lan;
            Process = new ProcessTakeAiming(this);
            InitializeComponent();
            InitializeCustomComponent();
        }

        //初始化窗体数据
        public override void InitData()
        {
            if(_aiming == null)
            {
                _aiming = new Aiming();
                _aiming.MeasuringCompleted += OnMeasuringCompleted;
                _aiming.Measurer.MeasuringAbnormal += OnMeasuringAbnormal;
                _aiming.Measurer.ErrorReceived += OnMeasuringErrorReceived;
                if (!_aiming.OpenService())
                {
                    MsgBox.Show("测距服务启动失败: " + _aiming.Error);
                }
            }
            
        }

        /// <summary>
        /// 保存窗体数据(窗体本次做完后, 进入下一步之前)
        /// </summary>
        public override void UpdateData()
        {

        }

        /// <summary>
        /// 保存窗体数据(窗体本次做完后, 进入下一步之前)
        /// </summary>
        public override void ClearData()
        {
            if (_aiming != null)
            {
                if (_aiming.IsComOpen)
                {
                    _aiming.Stop();
                    StopWork();
                }
            }          
            base.ClearData();            
        }

        public override void Close()
        {
            if (_aiming != null)
            {
                _aiming.CloseService();
            }
            base.Close();
        }

        public override void StartWork()
        {
            Speak("请保持身体坐直，平视前方");
            _aiming.Start();            
            _motion.StartWorking();
        }

        public override void StopWork()
        {
            if (_aiming != null)
            {
                _aiming.Stop();
            }            
        }

        protected override void btnBack_Click(object sender, EventArgs e)
        {
            _motion.StopWorking();
            base.btnBack_Click(sender, e);
        }

        protected override void btnCancel_Click(object sender, EventArgs e)
        {           
            _motion.StopWorking();
            Log.Write("用户取消了拍照.");
            NotifyClientTakePhotoCancell();
            ClearData();
            Process.ToHome();
        }

        private void NotifyClientTakePhotoCancell()
        {
            if (Process.Job != null)
            {
                Process.Job.Cancel("用户选择了取消拍照.");
            }
        }

        protected void OnMeasuringCompleted(object sender, MeasuringCompletedEventArgs e)
        {
            if (this.InvokeRequired)
            {
                MeasuringCompletedEventHandler Handler
                    = new MeasuringCompletedEventHandler(OnMeasuringCompleted);
                this.BeginInvoke(Handler, new object[]{sender, e});
                return;
           }
            StopWork();
            ClearData();
            Process.ToNext();
        }

        private void OnMeasuringErrorReceived(string error, int code)
        {
            if (this.InvokeRequired)
            {
                LadarErrorReceivedEventHandler Handler
                    = new LadarErrorReceivedEventHandler(OnMeasuringErrorReceived);
                this.BeginInvoke(Handler, new object[] { error, code });
                return;
            }
            Log.Write($"测距异常:代码-{code}, {error}");
            //MsgBox.Show($"测距异常:代码-{code}, {error}");
        }

        public override void OnCountdownCompleted(object sender, EventArgs e)
        {
            Log.Write("倒计时结束，中止拍照.");
            _motion.StopWorking();
            if (Process.Job != null)
            {
                Process.Job.Cancel("倒计时结束，调节镜头高度操作未完成，取消拍照.");
            }
            base.OnCountdownCompleted(sender, e);           
        }

        private void OnMeasuringAbnormal(int value)
        {
            if (this.InvokeRequired)
            {
                MeasuringUpdateDelegate Handler
                    = new MeasuringUpdateDelegate(OnMeasuringAbnormal);
                this.BeginInvoke(Handler, new object[] { value });
                return;
            }
            Speak("测量身高异常，请注意脚下站立到指定位置");
        }
    }
}
